package com.guodi.map.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.map.entity.MapLayer;
import com.guodi.map.entity.MapProjectDir;
import com.guodi.map.entity.MapProjectFile;
import com.guodi.map.mapper.MapProjectDirMapper;
import com.guodi.map.service.IMapProjectDirService;
import com.guodi.map.service.IMapProjectFileService;
import com.guodi.map.utils.RecursionUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @描述：专题目录/图层 服务类
 * @作者: 代鹏刚
 * @日期: 2019-12-11
 */
@Service
public class MapProjectDirServiceImpl extends ServiceImpl<MapProjectDirMapper, MapProjectDir> implements IMapProjectDirService {

    @Resource
    private MapProjectDirMapper mapProjectDirMapper;
    @Resource
    public IMapProjectFileService mapProjectFileService;

    @Override
    public List<MapProjectDir> listTreeByProjectIdAndUserId(MapProjectDir mapProjectDir) {
        return mapProjectDirMapper.listTreeByProjectIdAndUserId(mapProjectDir);
    }

    @Override
    public List<MapProjectDir> deleteByProjectId(String projectId) {
        return mapProjectDirMapper.deleteByProjectId(projectId);
    }

    @Override
    public List<MapProjectDir> deleteByIds(String[] ids) {
        return mapProjectDirMapper.deleteByIds(ids);
    }

    @Override
    public List<MapProjectDir> verifyByNames(String[] names, String parentId) {
        return mapProjectDirMapper.verifyByNames(names, parentId);
    }

    @Override
    public List<String> getDirNames(String[] ids) {
        return mapProjectDirMapper.getDirNames(ids);
    }

    @Override
    public List<MapProjectDir> getSpecialMenu(List<String> roleId, String projectId, String projectCode, String layerType, String parentDirId) {
        return mapProjectDirMapper.getSpecialMenu(roleId, projectId, projectCode, layerType,parentDirId);
    }

    @Override
    public List<MapProjectDir> getSpecialMenu(List<String> roleId, String projectId, String projectCode, String layerType) {
        return mapProjectDirMapper.getSpecialMenu(roleId, projectId, projectCode, layerType,null);
    }

    @Override
    public List<MapProjectDir> getSpecialMenuByParentId(List<String> roleId, String projectId, String projectCode, String layerType, String parentId) {
        return mapProjectDirMapper.getSpecialMenuByParentId(roleId, projectId, projectCode, layerType, parentId);
    }

    @Override
    public List<MapProjectDir> findByCode(String code) {
        return mapProjectDirMapper.findByCode(code);
    }

    @Override
    public List<MapProjectDir> listTreeByCodeAndParentId(String code , String parentId, boolean isDir) {

        List<MapProjectDir> dataList = mapProjectDirMapper.listTreeByCodeAndParentId(code,parentId,isDir);
        return dataList;
    }

    /**
    * @描述:递归创建树
    * @入参:
    * @出参:
    * @作者: 郑文彬
    * @日期: 2020/4/16 10:22
    **/
    @Override
    public List createTree(String pid,List<MapProjectDir> menus){
        List tree = new ArrayList<>();
        for (int i = 0; i < menus.size(); i++) {
            MapProjectDir mapProjectDir = menus.get(i);
            if(mapProjectDir.getParentId().equals(pid)){
                tree.add(mapProjectDir);
                List children = createTree(mapProjectDir.getId(), menus);
                mapProjectDir.setChildren(children);
            }
        }
        return  tree;
    }

    @Override
    public IPage<MapLayer> listLayerByDirId(Page<MapLayer> page, String dirId, String code, String keyword) {
        IPage<MapLayer>  list = mapProjectDirMapper.listLayerByDirId(page,dirId,code,keyword);
        return list;
    }

    /**
     * @描述: 获取有附件的所有专题节点
     * @入参:
     * @出参:
     * @作者: 陈宏成
     * @日期: 2021-04-28 09:48:00
     **/
    @Override
    public List<MapProjectDir> listProjectDirHasFile(List<String> roleId, String projectCode){
        List<MapProjectDir> mapProjectDirList = this.mapProjectDirMapper.listProjectDirHasFile(roleId, projectCode);
        // 处理别名问题
        mapProjectDirList.stream().forEach(dir -> {
            // 查询他们关联的附件树
            List<MapProjectFile> mapProjectFiles = mapProjectFileService.listTreeByEntity(dir.getId());
            // 构建附件树
            List<MapProjectFile> treeFile = RecursionUtil.buildMapProjectFileTree(mapProjectFiles, dir.getId());
            // 填充回去
            dir.setProjectFiles(treeFile);
        });
        return mapProjectDirList;
    }

    @Override
    public List<MapProjectDir> listTreeByProjectIdAndUserIdLazyLoad(MapProjectDir mapProjectDir) {
        return mapProjectDirMapper.listTreeByProjectIdAndUserIdLazyLoad(mapProjectDir);
    }

    @Override
    public void updateNameByIds(List<String> ids, String name) {
        mapProjectDirMapper.updateNameByIds(ids, name);
    }
}
